package com.martinwj.controller.api_info;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.martinwj.entity.*;
import com.martinwj.exception.SysException;
import com.martinwj.service.*;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: ApiInfoAction
 * @Description: TODO
 * @author: martin-wj
 * @createDate: 2021-04-05
 */
@Controller
@RequestMapping("api_info")
public class ApiInfoAction {
    @Autowired
    private ApiService apiService;
    @Autowired
    private FieldProfileService fieldProfileService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private FieldService fieldService;
    @Autowired
    private TagService tagService;

    /**
     * 查询所有幻灯片接口
     */
    @RequestMapping("list_slide.action")
    public String listSlide(ModelMap map) {

        List<Api> list = apiService.listByType("slide");
        map.put("list", list);

        return "admin/api_info/list_slide";
    }

    /**
     * 查询所有排行榜接口
     */
    @RequestMapping("list_rank.action")
    public String listRank(ModelMap map,
                           @RequestParam(value="pageNum", defaultValue="1") int pageNum,
                           @RequestParam(value="pageSize", defaultValue="10") int pageSize) {

        // pageHelper分页插件
        // 只需要在查询之前调用，传入当前页码，以及每一页显示多少条
        PageHelper.startPage(pageNum, pageSize);
        List<Api> list = apiService.listByType("rank");
        PageInfo<Api> pageInfo = new PageInfo<Api>(list);
        map.put("pageInfo", pageInfo);

        return "admin/api_info/list_rank";
    }

    /**
     * 排行榜接口配置
     */
    @RequestMapping("rank_edit.action")
    public String rankEdit(ModelMap map,
                           @RequestParam(value="id") String id,
                           @RequestParam(value="name") String name) {

        // 数据来源，分类
        List<Type> typeList = typeService.list();
        map.put("typeList", typeList);

        // 查询排行榜接口配置选项
        Map<String, Object> apiInfo = apiService.selectById(id);
        map.put("apiInfo", apiInfo);

        map.put("id", id);
        map.put("name", name);

        return "admin/api_info/rank_edit";
    }

    /**
     * 保存排行榜条件设置
     * @throws SysException
     */
    @RequestMapping("rank_save.json")
    @ResponseBody
    public Result rankSave(Api api) throws SysException {

        apiService.rankSave(api);

        return Result.success();
    }

    /**
     * 查询所有自定义接口
     */
    @RequestMapping("list_user.action")
    public String listUser(ModelMap map,
                           @RequestParam(value="pageNum", defaultValue="1") int pageNum,
                           @RequestParam(value="pageSize", defaultValue="10") int pageSize) {

        // pageHelper分页插件
        // 只需要在查询之前调用，传入当前页码，以及每一页显示多少条
        PageHelper.startPage(pageNum, pageSize);
        List<Api> list = apiService.listByType("user");
        PageInfo<Api> pageInfo = new PageInfo<Api>(list);
        map.put("pageInfo", pageInfo);

        return "admin/api_info/list_user";
    }

    /**
     * 用户自定义接口配置
     */
    @RequestMapping("user_edit.action")
    public String userEdit(ModelMap map,
                           @RequestParam(value="id") String id,
                           @RequestParam(value="name") String name) {

        // 数据来源，分类
        List<Type> typeList = typeService.list();
        map.put("typeList", typeList);

        // 查询该接口的信息
        Map<String, Object> apiInfo = apiService.selectById(id);
        String typeId = "";
        if (apiInfo.get("type_id")==null) {
            // 首次设置条件时，默认查询第一条分类的id
            typeId = typeList.get(0).getId();
        } else {
            typeId = apiInfo.get("type_id").toString();
        }

        // 查询单选框字段的变量名
        List<Field> radioList = fieldService.listRadioByTypeId(typeId);
        map.put("radioList", radioList);
        if (radioList!=null && !radioList.isEmpty()) {
            for (Field field : radioList) {
                List<FieldProfile> list = fieldProfileService.listByFieldId(field.getId());
                field.setFieldProfileList(list);
            }
        }

        // 标签
        if (apiInfo.get("tag")!=null) {
            // 将标签字符串分割成数组形式
            String[] tagIdArr = apiInfo.get("tag").toString().split(",");

            // 查询这些标签信息
            List<String> tagList = tagService.listNameByIdArr(tagIdArr);
            if (tagList!=null && !tagList.isEmpty()) {
                // 将标签集合转为json格式，方便js调用
                JSONArray jsonTagList = JSONArray.fromObject(tagList);
                map.put("jsonTagList", jsonTagList);
            }
        }

        map.put("id", id);
        map.put("name", name);
        map.put("apiInfo", apiInfo);

        return "admin/api_info/user_edit";
    }

    /**
     * 保存自定义数据条件设置
     * @throws SysException
     */
    @RequestMapping("user_save.json")
    @ResponseBody
    public Result userSave(HttpServletRequest request) throws SysException {
        Map<String, Object> param = new HashMap<String, Object>();

        // 该接口需要用到哪些字段
        String field = "";

        String id = request.getParameter("id");
        param.put("id", id);

        // 视频来源：电影、动漫等分类id
        String typeId = request.getParameter("type_id");
        param.put("type_id", typeId);
        field += "type_id";

        // 查询单选框字段的变量名
        List<Field> radioList = fieldService.listRadio();
        if (radioList!=null && radioList.isEmpty()==false) {
            for (Field fieldInfo : radioList) {
                String varName = fieldInfo.getVarName();// 字段变量名

                String[] arr = request.getParameterValues(varName);
                if (arr!=null && arr.length>0) {
                    // 将数组转为 xx,xx 形式的字符串（因为StringUtils引了别的包，不支持StringUtils.join(arr, ",");，所以这里我们手写）
                    String str = "";
                    for (int i=0; i<arr.length; i++) {
                        if (i==0) {
                            str = arr[i];
                        } else {
                            str = str + "," + arr[i];
                        }
                    }
                    param.put(varName, str);
                    field += "," + varName;
                }
            }
        }

        param.put("field", field);
        param.put("num", request.getParameter("num"));
        param.put("tag", request.getParameter("tag"));
        param.put("select_video", request.getParameter("select_video"));
        param.put("cache_time", request.getParameter("cache_time"));

        apiService.userSave(radioList, param);

        return Result.success();
    }

    /**
     * 保存接口
     * @param type 接口类型
     * @param idArr 接口主键数组
     * @param sortArr 接口排序数组
     * @param nameArr 接口名称数组
     */
    @RequestMapping("save.json")
    @ResponseBody
    public Result save(
            @RequestParam(value="type") String type,
            @RequestParam(value="idArr") String[] idArr,
            @RequestParam(value="sortArr") String[] sortArr,
            @RequestParam(value="nameArr") String[] nameArr) {

        List<Api> apiInfoList = new ArrayList<Api>();

        // 判断是否已有既存数据
        if (idArr.length==0) {
            // 全是新增
            // 遍历sortArr数组
            for (int i=0; i<sortArr.length; i++) {
                Api api = new Api();
                api.setSort(sortArr[i]);
                api.setName(nameArr[i]);
                api.setType(type);

                apiInfoList.add(api);
            }
        } else {
            // 遍历idArr数组
            for (int i=0; i<idArr.length; i++) {
                Api api = new Api();
                api.setId(idArr[i]);
                api.setSort(sortArr[i]);
                api.setName(nameArr[i]);
                api.setType(type);

                apiInfoList.add(api);
            }
        }

        apiService.save(apiInfoList);

        return Result.success();
    }

    /**
     * 删除
     * @param idArr 接口主键数组
     */
    @RequestMapping("delete.json")
    @ResponseBody
    public Result delete(
            @RequestParam(value="idArr") String[] idArr) {

        apiService.delete(idArr);

        return Result.success();
    }

    /**
     * 根据主键，获取接口信息
     * @param id 主键
     * @return
     */
    @RequestMapping("get_api_info.json")
    @ResponseBody
    public Result getApiInfo(@RequestParam(value="id") String id) {

        Map<String, Object> apiInfo = apiService.selectById(id);

        return Result.success().add("apiInfo", apiInfo);
    }
}
